import 'package:basic_common/common_exports.dart';
import 'package:basic_profile/model/article_data_list.dart';
import 'package:basic_profile/model/article_title_list_data.dart';

class MyAttentionController extends GetxController
    with GetTickerProviderStateMixin {
  TabController? tabController;
  final todoTitleList = ["文章", "问答", "动态"];
  final titleDataList = <ArticleTitleData>[].obs;
  final articleList = <ArticleList>[].obs;
  final title = ''.obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    tabController = TabController(
        initialIndex: 0, length: titleDataList.length, vsync: this)
      ..addListener(() {
        if (!tabController!.indexIsChanging) {
          int pageIndex = tabController!.index;
          int artId = titleDataList[pageIndex].id!;
          getArticleList(artId);
        }
      });
    title.value = Get.arguments['title'];
    String hosNo = Get.arguments['hosNo'];
    getTabTitles(hosNo);
  }

  /// titleList
  Future getTabTitles(String hosN) async {
    HttpData res = await ApiService().getArticleTitleList(
        data: {"columnMark": "healthEducation", "hospNo": hosN});
    if (res.code == 1) {
      List titleList = res.data;
      List newList = titleList
          .where((element) => element['columnTitle'] == '健康宣教')
          .toList();
      if (newList.isNotEmpty) {
        List newChils = newList[0]['childList'];
        // List newChils = childList
        //     .where((element) => todoTitleList.contains(element['columnTitle']))
        //     .toList();
        newChils.forEach((element) {
          ArticleTitleData articleTitleData =
              ArticleTitleData.fromJson(element);
          titleDataList.add(articleTitleData);
        });
        tabController = TabController(
            initialIndex: 1, length: titleDataList.length, vsync: this)
          ..addListener(() {
            if (!tabController!.indexIsChanging) {
              int pageIndex = tabController!.index;
              int artId = titleDataList[pageIndex].id!;
              getArticleList(artId);
            }
          });
        ;
        tabController!.animateTo(0);
      }
    }
  }

  /// 文章列表
  getArticleList(int id) async {
    articleList.value = [];
    HttpData res = await ApiService().getArticleList(data: {"id": id});
    if (res.code == 1) {
      ArticleData articleData = ArticleData.fromJson(res.data);
      if (articleData.total! > 0) {
        articleList.value = articleData.list!;
      }
    }
    update(['articleList']);
  }
}
